/**************************************************************************
 * @file usr_buck_boost_ctrl.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-07-18
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#include "usr_cfg.h"
#include "usr_buck_boost_ctrl.h"
#include "usr_adc_handle.h"
#include "usr_hrpwm_handle.h"
#include <math.h>

// 三角波的范围    0~38461
// ADC最大值：    0~4096

void usr_set_output_vol(UsrBuckBoostDatT *buck_boost,uint32_t target_vol){
    buck_boost->ref_vo = GET_ADC_VOLUE_BY_OUT_VOL(target_vol);
    // DDL_Printf("Set vo-adc-value:%d\n",buck_boost->ref_vo);
}
void usr_set_output_current(UsrBuckBoostDatT *buck_boost,uint32_t target_curr/*mA*/){
    buck_boost->ref_io = GET_ADC_VOLUE_BY_OUT_CURR(target_curr);
    // DDL_Printf("Set io-adc-value:%d\n",buck_boost->ref_io);
}

RAM_FUNC float get_init_pi_output_add_value(WorkModeT curr_wk_mode, WorkModeT next_wk_mode, float current_fixed_duty, float adj_duty) {
    float pi_init_out_add_value = ADC_MAP_PWM_MIN_DUTY;

    switch (curr_wk_mode) {
        case BUCK_W_MODE:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = (adj_duty);
            }
            break;

        case BUCK_BOOST_W_MIX_DW_M:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (adj_duty);
            }
            break;

        case BUCK_BOOST_W_MIX_UP_M:
            if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = (adj_duty);
            }
            break;

        case BOOST_W_MODE:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = (adj_duty);
            } else if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            }
            break;
        default:
            break;
    }
    return pi_init_out_add_value;
}

RAM_FUNC void usr_buck_boost_work_mode_handle(UsrBuckBoostDatT *pbuck_boost, uint16_t in_vol_adc_value, uint16_t out_vol_adc_value, uint16_t out_curr_adc_value) {
    UsrBuckBoostDatT *buck_boost = pbuck_boost;
    float buck_duty = BOOST_MINI_DUTY_CYCLE, boost_duty = BUCK_MINI_DUTY_CYCLE;
    WorkModeT wk_mode;
    HRPWM_CmpDat_T buck_cmp_value, boost_cmp_value;
    float vo = out_vol_adc_value;
    float io = out_curr_adc_value;
    /* 如果输出电压>(输入电压+1V),Buck则不工作，进入最大占空比模式，仅仅给自举电容充电*/
    /******************滞回比较器，避免在边界处频繁切换*********************** *******************************************************/
    switch (buck_boost->wk_mode) {
        case BUCK_W_MODE:
            if (in_vol_adc_value > (buck_boost->ref_vo + GET_ADC_VOLUE_BY_OUT_VOL(1000u))) { /*输入电压大于输出电压1V*/
                wk_mode = BUCK_W_MODE;
            } else {
                wk_mode = BUCK_BOOST_W_MIX_DW_M;
            }
            break;
        case BUCK_BOOST_W_MIX_DW_M:
            if (in_vol_adc_value > (buck_boost->ref_vo + GET_ADC_VOLUE_BY_OUT_VOL(1200u))) { /*输入电压大于输出电压1.2V*/
                wk_mode = BUCK_W_MODE;
            } else if (in_vol_adc_value > (buck_boost->ref_vo + GET_ADC_VOLUE_BY_OUT_VOL(200u))) { /*输入电压大于输出电压0.2V*/
                wk_mode = BUCK_BOOST_W_MIX_DW_M;
            } else {
                wk_mode = BUCK_BOOST_W_MIX_UP_M;
            }
            break;
        case BUCK_BOOST_W_MIX_UP_M:
            if (in_vol_adc_value > (buck_boost->ref_vo + GET_ADC_VOLUE_BY_OUT_VOL(300u))) { /*输入电压大于输出电压0.3V*/
                wk_mode = BUCK_BOOST_W_MIX_DW_M;
            } else if (in_vol_adc_value > (buck_boost->ref_vo - GET_ADC_VOLUE_BY_OUT_VOL(1200u))) { /*输入电压小于输出电压在1.2V以内时*/
                wk_mode = BUCK_BOOST_W_MIX_UP_M;
            } else {
                wk_mode = BOOST_W_MODE;
            }
            break;
        case BOOST_W_MODE:
            if (in_vol_adc_value > (buck_boost->ref_vo - GET_ADC_VOLUE_BY_OUT_VOL(1000u))) { /*输入电压小于输出电压在1V以内时*/
                wk_mode = BUCK_BOOST_W_MIX_UP_M;
            } else {
                wk_mode = BOOST_W_MODE;
            }
            break;
        default:
            wk_mode = BUCK_W_MODE;
            break;
    }
    buck_boost->next_wk_mode = wk_mode;
    /****************************************************************************************************** */
    /* float duty;*/
    float vo_err_add, io_err_add;
    /* 电压环*/
    vo_err_add = usr_PID_Compute(&buck_boost->pid_vo, buck_boost->ref_vo, vo);
    /* 电流环*/
    /*占空比=(误差ADC_VALUE/ADC_ACCURACY)，脉宽=占空比*周期*/
    /****************************恒压恒流决策处理*******双环竞争************************************** */
    if (1) {
        /***************恒压恒流没有完善，暂时用恒压一种处理机制******************************************* */
        buck_duty = buck_boost->pwm_duty+vo_err_add;

        if(buck_duty > BUCK_MAX_DUTY_CYCLE){
            buck_duty = BUCK_MAX_DUTY_CYCLE;
        }else if(buck_duty<BUCK_MINI_DUTY_CYCLE){
            buck_duty = BUCK_MINI_DUTY_CYCLE;
        }
        buck_boost->pwm_duty = buck_duty;
        /********************************************** */
    } else {
        usr_PID_Compute(&buck_boost->pid_io, buck_boost->ref_io, io);
        if (vo > (buck_boost->ref_vo * 1.02f)) {
            buck_duty = vo_err_add;
            main_dat.cv_cc_flg = CV_ACTIVE;
        } else if (vo_err_add > io_err_add) {
            buck_duty = io_err_add;
            main_dat.cv_cc_flg = CC_ACTIVE;
        } else {
            buck_duty = vo_err_add;
            main_dat.cv_cc_flg = CV_ACTIVE;
        }
    }
    /************************************************************************** */
    
    boost_duty = buck_duty;
    switch (buck_boost->wk_mode) {
        case BUCK_W_MODE: {
            float fixed_duty = BOOST_MINI_DUTY_CYCLE;
            if (buck_duty >= BUCK_MAX_DUTY_CYCLE && wk_mode != buck_boost->wk_mode && io < buck_boost->ref_io) {
                buck_boost->wk_mode = wk_mode;
                DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BUCK_W_MODE, buck_boost->wk_mode, fixed_duty, buck_duty));
            }
            boost_duty = fixed_duty;

        } break;
        case BUCK_BOOST_W_MIX_DW_M: {
            float fixed_duty = BOOST_MINI_DUTY_CYCLE + 0.1f;
            if ((buck_duty >= BUCK_MAX_DUTY_CYCLE || buck_duty <= 0.85f)) {
                if (wk_mode != buck_boost->wk_mode) {
                    buck_boost->wk_mode = wk_mode;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BUCK_BOOST_W_MIX_DW_M, buck_boost->wk_mode, fixed_duty, buck_duty));
                } else if (io > buck_boost->ref_io) {
                    buck_boost->wk_mode = BUCK_W_MODE;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BUCK_BOOST_W_MIX_DW_M, buck_boost->wk_mode, fixed_duty, buck_duty));
                }
            }
            boost_duty = fixed_duty;
        } break;
        case BUCK_BOOST_W_MIX_UP_M: {
            float fixed_duty = BUCK_MAX_DUTY_CYCLE - 0.1f;
            if ((boost_duty >= 0.3 || boost_duty <= BOOST_MINI_DUTY_CYCLE)) {
                if (wk_mode != buck_boost->wk_mode) {
                    buck_boost->wk_mode = wk_mode;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BUCK_BOOST_W_MIX_UP_M, buck_boost->wk_mode, fixed_duty, boost_duty));
                } else if (io > buck_boost->ref_io) {
                    buck_boost->wk_mode = BUCK_W_MODE;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BUCK_BOOST_W_MIX_UP_M, buck_boost->wk_mode, fixed_duty, boost_duty));
                }
            }
            buck_duty = fixed_duty;
        } break;
        case BOOST_W_MODE: {
            float fixed_duty = BUCK_MAX_DUTY_CYCLE;
            if (boost_duty <= BOOST_MINI_DUTY_CYCLE) {
                if (wk_mode != buck_boost->wk_mode) {
                    buck_boost->wk_mode = wk_mode;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BOOST_W_MODE, buck_boost->wk_mode, fixed_duty, boost_duty));
                } else if (io > buck_boost->ref_io) {
                    buck_boost->wk_mode = BUCK_W_MODE;
                    DDL_Printf("WorkMode:%d n", buck_boost->wk_mode);
                    usr_PID_Reset(&buck_boost->pid_vo, get_init_pi_output_add_value(BOOST_W_MODE, buck_boost->wk_mode, fixed_duty, boost_duty));
                }
            }
            buck_duty = fixed_duty;
        } break;
        default:
            break;
    }
    {
        HRPWM_CmpDat_T cmp_dat = {0xc1, 0xc1, 0xc1, 0xc1};
        cmp_dat.cmp_a_value = (1.0f - buck_duty) * buck_boost->buck_period;
        cmp_dat.cmp_b_value = buck_boost->buck_period - 65;
        if (cmp_dat.cmp_b_value > (buck_boost->buck_period - 65)) cmp_dat.cmp_b_value = buck_boost->buck_period - 65;
        cmp_dat.cmp_e_value = cmp_dat.cmp_a_value + ((cmp_dat.cmp_b_value - cmp_dat.cmp_a_value) >> 1);
        buck_cmp_value = cmp_dat;
    }
    {
        HRPWM_CmpDat_T cmp_dat = {0xc1, 0xc1, 0xc1, 0xc1};

        cmp_dat.cmp_a_value = buck_cmp_value.cmp_a_value + HRPWM_GET_DEADTIME(50u);
        cmp_dat.cmp_b_value = cmp_dat.cmp_a_value + (boost_duty)*buck_boost->boost_period;
        if (cmp_dat.cmp_b_value > (buck_boost->boost_period - 65)) cmp_dat.cmp_b_value = buck_boost->boost_period - 65;
        boost_cmp_value = cmp_dat;
    }
    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_a_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMCR, buck_cmp_value.cmp_a_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_b_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMDR, buck_cmp_value.cmp_b_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_e_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMGR, buck_cmp_value.cmp_e_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_f_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMHR, buck_cmp_value.cmp_f_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_a_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMCR, boost_cmp_value.cmp_a_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_b_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMDR, boost_cmp_value.cmp_b_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_e_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMGR, boost_cmp_value.cmp_e_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_f_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMHR, boost_cmp_value.cmp_f_value);


}
void usr_buck_boost_pid_init(UsrBuckBoostDatT *buck_boost){
    usr_PID_Init(&buck_boost->pid_vo,0.00002f,0.00003f,/*ADC_SAMPLE_PERIOD,*/ADC_MAP_PWM_MIN_DUTY,ADC_MAP_PWM_MAX_DUTY,ADC_MAP_PWM_INC_MAX);
    usr_PID_Init(&buck_boost->pid_io,0.00002f,0.00003f,/*ADC_SAMPLE_PERIOD,*/ADC_MAP_PWM_MIN_DUTY,ADC_MAP_PWM_MAX_DUTY,ADC_MAP_PWM_INC_MAX);
    buck_boost->pwm_duty = BUCK_MINI_DUTY_CYCLE;
}




RAM_FUNC int32_t get_init_int_pi_output_add_value(WorkModeT curr_wk_mode, WorkModeT next_wk_mode, int32_t current_fixed_duty, int32_t adj_duty) {
    int32_t pi_init_out_add_value = BUCK_MINI_DUTY_CMP_VALUE;

    switch (curr_wk_mode) {
        case BUCK_W_MODE:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = current_fixed_duty;
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = current_fixed_duty;
            } else if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = adj_duty;
            }
            break;

        case BUCK_BOOST_W_MIX_DW_M:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (adj_duty);
            }
            break;

        case BUCK_BOOST_W_MIX_UP_M:
            if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BOOST_W_MODE) {
                pi_init_out_add_value = (adj_duty);
            }
            break;

        case BOOST_W_MODE:
            if (next_wk_mode == BUCK_BOOST_W_MIX_UP_M) {
                pi_init_out_add_value = (adj_duty);
            } else if (next_wk_mode == BUCK_BOOST_W_MIX_DW_M) {
                pi_init_out_add_value = (current_fixed_duty);
            } else if (next_wk_mode == BUCK_W_MODE) {
                pi_init_out_add_value = (current_fixed_duty);
            }
            break;
        default:
            break;
    }
    return pi_init_out_add_value;
}

uint32_t debug_cnt=0;

RAM_FUNC void usr_buck_boost_work_mode_handle_int(UsrBuckBoostDatT *pbuck_boost, uint16_t in_vol_adc_value, uint16_t out_vol_adc_value, uint16_t out_curr_adc_value) {
    UsrBuckBoostDatT *buck_boost = pbuck_boost;
    int32_t buck_duty,boost_duty;
    WorkModeT wk_mode;
    HRPWM_CmpDat_T buck_cmp_value, boost_cmp_value;
    int32_t vo = out_vol_adc_value;
    int32_t io = out_curr_adc_value;
    /* 如果输出电压>(输入电压+1V),Buck则不工作，进入最大占空比模式，仅仅给自举电容充电*/
    /******************滞回比较器，避免在边界处频繁切换*********************** *******************************************************/

    /****************************************************************************************************** */
    /* float duty;*/
    int32_t vo_err_add, io_err_add;
    /* 电压环*/
    vo_err_add = usr_pi_int_compute(&buck_boost->int_pi_vo, buck_boost->ref_vo, vo);
    /* 电流环*/
    /*占空比=(误差ADC_VALUE/ADC_ACCURACY)，脉宽=占空比*周期*/
    /****************************恒压恒流决策处理*******双环竞争************************************** */
    if (1) {
        /***************恒压恒流没有完善，暂时用恒压一种处理机制******************************************* */
        buck_duty = buck_boost->pwm_cmp_value + vo_err_add;

        if (buck_duty > BUCK_MAX_DUTY_CMP_VALUE) {
            buck_duty = BUCK_MAX_DUTY_CMP_VALUE + 1;
        } else if (buck_duty < BUCK_MINI_DUTY_CMP_VALUE) {
            buck_duty = BUCK_MINI_DUTY_CMP_VALUE - 1;
        }
        buck_boost->pwm_cmp_value = buck_duty;
        /********************************************** */
    } else {
        // usr_PID_Compute(&buck_boost->pid_io, buck_boost->ref_io, io);
        // if (vo > (buck_boost->ref_vo * 1.02f)) {
        //     buck_duty = vo_err_add;
        //     main_dat.cv_cc_flg = CV_ACTIVE;
        // } else if (vo_err_add > io_err_add) {
        //     buck_duty = io_err_add;
        //     main_dat.cv_cc_flg = CC_ACTIVE;
        // } else {
        //     buck_duty = vo_err_add;
        //     main_dat.cv_cc_flg = CV_ACTIVE;
        // }
    }
    /************************************************************************** */

    if(buck_duty < (BUCK_MAX_DUTY_CMP_VALUE/2)){
        boost_duty = BUCK_MINI_DUTY_CMP_VALUE;
    }else{
        boost_duty = buck_duty - BUCK_MAX_DUTY_CMP_VALUE/2 ;
        buck_duty = (BUCK_MAX_DUTY_CMP_VALUE/2);
        if(boost_duty < BUCK_MINI_DUTY_CMP_VALUE)boost_duty=BUCK_MINI_DUTY_CMP_VALUE;
    }

    {
        HRPWM_CmpDat_T cmp_dat = {0xc1, 0xc1, 0xc1, 0xc1};
        cmp_dat.cmp_a_value = (buck_boost->buck_period - buck_duty);
        cmp_dat.cmp_b_value = buck_boost->buck_period - 65;
        if (cmp_dat.cmp_b_value > (buck_boost->buck_period - 65)) cmp_dat.cmp_b_value = buck_boost->buck_period - 65;
        cmp_dat.cmp_e_value = cmp_dat.cmp_a_value + ((cmp_dat.cmp_b_value - cmp_dat.cmp_a_value) >> 1);
        buck_cmp_value = cmp_dat;
    }
    {
        HRPWM_CmpDat_T cmp_dat = {0xc1, 0xc1, 0xc1, 0xc1};

        cmp_dat.cmp_a_value = buck_cmp_value.cmp_a_value + HRPWM_GET_DEADTIME(50u);
        cmp_dat.cmp_b_value = cmp_dat.cmp_a_value + boost_duty;
        if (cmp_dat.cmp_b_value > (buck_boost->boost_period - 65)) cmp_dat.cmp_b_value = buck_boost->boost_period - 65;
        boost_cmp_value = cmp_dat;
    }
    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_a_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMCR, buck_cmp_value.cmp_a_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_b_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMDR, buck_cmp_value.cmp_b_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_e_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMGR, buck_cmp_value.cmp_e_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BUCK_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(buck_cmp_value.cmp_f_value));
    WRITE_REG32(HRPWM_BUCK_PWM_UNIT->HRGCMHR, buck_cmp_value.cmp_f_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_a_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMCR, boost_cmp_value.cmp_a_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_b_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMDR, boost_cmp_value.cmp_b_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_e_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMGR, boost_cmp_value.cmp_e_value);

    DDL_ASSERT(IS_HRPWM_UNIT(HRPWM_BST_PWM_UNIT));
    DDL_ASSERT(IS_HRPWM_DATA_REG_RANGE1(boost_cmp_value.cmp_f_value));
    WRITE_REG32(HRPWM_BST_PWM_UNIT->HRGCMHR, boost_cmp_value.cmp_f_value);
}

void usr_buck_boost_int_pid_init(UsrBuckBoostDatT *buck_boost){
    usr_pi_int_Init(&buck_boost->int_pi_vo,10.0f,0.29f,PI_CTRL_INT_MIN,PI_CTRL_INT_MAX,PI_CTRL_INT_INC_MAX);
    usr_pi_int_Init(&buck_boost->int_pi_io,10.0f,0.29f,PI_CTRL_INT_MIN,PI_CTRL_INT_MAX,PI_CTRL_INT_INC_MAX);
    buck_boost->pwm_cmp_value = BUCK_MINI_DUTY_CMP_VALUE;
}

